local sk__shayi = fk.CreateSkill {

  name = "sk__shayi",

  tags = { Skill.Compulsory, },

}



sk__shayi:addEffect("viewas", {
  name = "sk__shayi",
  prompt = "#sk__shayi",
  card_filter = function(self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected == 0 and card.color == Card.Black and card:getMark("@@sk__shayi-phase") > 0
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("slash")
    card:addSubcard(cards[1])
    card.skillName = sk__shayi.name
    return card
  end,
  before_use = function(self, player, use)
    player:drawCards(1, sk__shayi.name)
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes("sk__shayi", Player.HistoryPhase) > 0 and
        table.find(player:getCardIds("h"), function(id)
          return Fk:getCardById(id):getMark("@@sk__shayi-phase") > 0
        end)
  end,
})

sk__shayi:addEffect(fk.EventPhaseStart, {
  name = "#sk__shayi_trigger",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__shayi.name) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__shayi")
    room:notifySkillInvoked(player, "sk__shayi", "special")
    player:drawCards(1, "sk__shayi")
    local cards = table.simpleClone(player:getCardIds("h"))
    if not player.dead and not player:isKongcheng() then
      room:setPlayerMark(player, "sk__shayi-phase", cards)
      for _, id in ipairs(cards) do
        if Fk:getCardById(id).color == Card.Black then
          room:setCardMark(Fk:getCardById(id, true), "@@sk__shayi-phase", 1)
        end
      end
    end
  end,
})
sk__shayi:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("sk__shayi-phase") ~= 0 then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("sk__shayi-phase")
    for _, move in ipairs(data) do
      if move.toArea ~= Card.Processing then
        for _, info in ipairs(move.moveInfo) do
          table.removeOne(mark, info.cardId)
          room:setCardMark(Fk:getCardById(info.cardId), "@@sk__shayi-phase", 0)
        end
      end
    end
    room:setPlayerMark(player, "sk__shayi-phase", mark)
  end,
})
sk__shayi:addEffect(fk.Death, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("sk__shayi-phase") ~= 0 then
      return player == target
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("sk__shayi-phase")
    for _, id in ipairs(mark) do
      room:setCardMark(Fk:getCardById(id), "@@sk__shayi-phase", 0)
    end
  end,
})

sk__shayi:addEffect("targetmod", {
  name = "#sk__shayi_targetmod",
  --sk__shayi,
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(sk__shayi.name) and skill.trueName == "slash_skill"
  end,
  bypass_distances = function(self, player, skill, card, to)
    return player:hasSkill(sk__shayi.name) and skill.trueName == "slash_skill"
  end,
})

return sk__shayi
